package app.qrcode.browser.chat.ui.dashboard

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import app.qrcode.browser.chat.adapter.Contact
import app.qrcode.browser.chat.adapter.ContactAdapter
import app.qrcode.browser.chat.databinding.FragmentDashboardBinding
import app.qrcode.browser.chat.model.Model
import app.qrcode.browser.chat.model.bean.UserInfo

class DashboardFragment : Fragment() {

    private var _binding: FragmentDashboardBinding? = null

    private var contactList = ArrayList<Contact>()

    private val contactAdapter by lazy { ContactAdapter() }

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        ViewModelProvider(this).get(DashboardViewModel::class.java)
        _binding = FragmentDashboardBinding.inflate(inflater, container, false)
        val root: View = binding.root
        val layoutManager = LinearLayoutManager(context)
        binding.contactlist.layoutManager = layoutManager
        binding.contactlist.adapter=contactAdapter
        return root
    }

    private fun initRecyclerView() {
        contactList.add(Contact("新的朋友", null, Contact.ITEM_ADD))
        contactList.add(Contact("群组",null,Contact.ITEM_QUN))

        val contactPeople:List<UserInfo> = Model.dbManager.contactTableDao.getContacts()
        for (contactPerson in contactPeople) {
            contactList.add(Contact(contactPerson.hxId,null,Contact.ITEM_NOHEAD))
        }
        contactAdapter.updateList(contactList)
        Log.w("cdm", "DashboardFragment.initRecyclerView -> $contactList")
    }

    override fun onResume() {
        super.onResume()
        initRecyclerView()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}